package com.priject.dao.impl;

import com.priject.bean.StudentBean;
import com.priject.dao.IStudentDao;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StudentDaoImpl extends BaseDao implements IStudentDao {
    @Override
    public void add(StudentBean student) {
        SqlSession session = this.getSqlSession();
        //执行添加语句，第一个参数为命名空间.语句块的id号，第二个参数为传入语句块的实参
        session.insert("studentScope.add",student);
        session.commit();
        session.close();
    }

    @Override
    public void del(int id) {
        SqlSession session = this.getSqlSession();
        //执行添加语句，第一个参数为命名空间.语句块的id号，第二个参数为传入语句块的实参
        session.delete("studentScope.del",id);
        session.commit();
        session.close();
    }

    @Override
    public void update(int id, String tel) {
            Map map = new HashMap<>();
            map.put("sid",id);
            map.put("newTel",tel);
        SqlSession session = this.getSqlSession();
        //执行添加语句，第一个参数为命名空间.语句块的id号，第二个参数为传入语句块的实参
        session.update("studentScope.update",map);
        session.commit();
        session.close();
    }

    @Override
    public List<StudentBean> findALL() {
        SqlSession session = this.getSqlSession();
        //执行添加语句，第一个参数为命名空间.语句块的id号，第二个参数为传入语句块的实参
        List<StudentBean> list = session.selectList("studentScope.findAll",null);
        session.close();
        return list;
    }

    @Override
    public StudentBean findById(int id) {

        SqlSession session = this.getSqlSession();
        //执行添加语句，第一个参数为命名空间.语句块的id号，第二个参数为传入语句块的实参
        StudentBean stu = session.selectOne("studentScope.findById",id);
        session.close();
        return stu;
    }
    public static void main(String[] args){
        IStudentDao dao = new StudentDaoImpl();
//        dao.add(new StudentBean("刘斯","18785648523", LocalDate.parse("1995-08-26")));
//        dao.del(12);
//        dao.update(11,"18999999999");
//        System.out.println(dao.findALL());
         System.out.println(dao.findById(10));
    }
}
